#ifndef _COMMOM_FUNC_H_
#define _COMMOM_FUNC_H_
#include "stdio.h"
#include "tchar.h"

typedef struct _defi_act_data_{
	int packetSize;
	int packetType;
	int mouseX;
	int mouseY;
	int dataSize;
	char packetData[4];
}defi_act_data,*pdefi_act_data;

#ifdef _UNICODE
#define _tcsprintf wsprintf
#else
#define _tcsprintf sprintf
#endif

__inline void TrimSpaceA(char *str)
{
	int len = strlen(str);
	len--;
	while(str[len] == 32 || str[len] == 10 || str[len] == 13){
		str[len] = 0;
		len--;
	}
	len = strlen(str);
	int pos = 0;
	while(str[pos] == 32){
		pos++;
	}
	if(pos > 0){
		strcpy(str, str+pos);
		str[len - pos - 1] = 0;
	}

}

__inline BOOL readFileToBuf(const TCHAR *filename, void *buf, int buflen)
{
	if(!filename || !buf || buflen == 0)return FALSE;
	FILE *fp = _tfopen(filename, TEXT("rb"));
	if(fp){
		memset(buf, 0, buflen);
		buflen = fread(buf, 1, buflen, fp);
		fclose(fp);
		return TRUE;
	}
	return FALSE;
}

__inline char *readFileToBuf(const TCHAR *filename, int *outlen = NULL)
{
	if(!filename)return NULL;
	FILE *fp = _tfopen(filename, TEXT("rb"));
	if(fp){
		char *filedata = NULL;
		int filelen = 0;
		fseek(fp, 0, SEEK_END);
		filelen = ftell(fp);
		fseek(fp, 0, SEEK_SET);
		filedata = (char *)malloc(filelen);
		memset(filedata, 0, filelen);
		fread(filedata, 1, filelen, fp);
		fclose(fp);
		if(outlen)*outlen = filelen;
		return filedata;
	}
	return NULL;
}

__inline BOOL GetExecuteFileDirectory(TCHAR *exe_path, int buflen)
{
	TCHAR *tmpstr;
	GetModuleFileName(NULL, exe_path, buflen);
	tmpstr = _tcsrchr(exe_path, '\\');
	*(tmpstr + 1) = '\0';
	return TRUE;
}

__inline BOOL writeBufToFile(const TCHAR *fileName, const char *buf, int buflen)
{
	if(fileName == NULL || buf == NULL || buflen <= 0){
		return FALSE;
	}
	FILE *fp = _tfopen(fileName, TEXT("wb"));
	if(fp){
		fwrite(buf, buflen, 1, fp);
		fclose(fp);
		return TRUE;
	}
	return FALSE;
}

__inline BOOL FileIsExist(const TCHAR *filename)
{
	if(GetFileAttributes(filename) == 0xFFFFFFFF){
		return FALSE;
	}
	return TRUE;
}

__inline const TCHAR *BackFile(const TCHAR *filename, const TCHAR *backDir = NULL)
{
	TCHAR bakpath[MAX_PATH], *tmpstr;
	SYSTEMTIME systime;
	_tcscpy(bakpath, filename);
	tmpstr = _tcsrchr(bakpath, '\\');
	if(tmpstr)*(tmpstr + 1) = 0;
	else *bakpath = 0;
	if(backDir)_tcscat(bakpath, backDir);
	tmpstr = bakpath + _tcslen(bakpath);
	if(!FileIsExist(bakpath)){
		if(!CreateDirectory(bakpath, NULL)){
			return NULL;
		}
	}
	GetLocalTime(&systime);
#ifdef _UNICODE
	wsprintf(tmpstr, _T("\\%d%d%d%d%d%d"), systime.wYear, systime.wMonth, systime.wDay, systime.wHour, systime.wMinute,
		systime.wSecond);
#else
	sprintf(tmpstr, "%d%d%d%d%d%d", systime.wYear, systime.wMonth, systime.wDay, systime.wHour, systime.wMinute,
		systime.wSecond);
#endif
	_tcscat(tmpstr, filename + _tcslen(filename) - 4);
	CopyFile(filename, bakpath, TRUE);
	return NULL;
}

__inline HWND wCreateWindow(WNDPROC WndProc, TCHAR *tcWindowName = NULL, TCHAR *tcClassName = NULL)
{
	WNDCLASSEX wcex;
	TCHAR szWindowClass[100] = TEXT("wWindowClass");
	TCHAR szWindowName[100] = TEXT("wWindowName");
	HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
	if(tcWindowName){
		_tcscpy(szWindowName, tcWindowName);
	}

	if(tcClassName){
		_tcscpy(szWindowClass, tcClassName);
	}

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= NULL;
	if(!RegisterClassEx(&wcex))
		return NULL;
	return CreateWindow(szWindowClass, szWindowName, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
}

__inline BOOL SetKeyValue(HKEY hkey, LPCTSTR subkey, LPCTSTR lpValueName, DWORD dwType, const BYTE* lpData, DWORD cbData)
{
	BOOL retValue = FALSE;
	if(RegCreateKey(hkey, subkey, &hkey) != ERROR_SUCCESS){
		return FALSE;
	}

	if(RegSetValueEx(hkey, lpValueName, NULL, dwType, lpData, cbData) == ERROR_SUCCESS){
		retValue = TRUE;
	}
	RegCloseKey(hkey);
	return retValue;
	return TRUE;
}

__inline BOOL GetKeyValue(HKEY hkey, LPCTSTR subkey, LPCTSTR lpValueName, DWORD dwType, void* lpData, DWORD cbData)
{
	int retValue = FALSE;
	DWORD dtype = dwType;

	if(RegOpenKey(hkey, subkey, &hkey) != ERROR_SUCCESS){
		return FALSE;
	}
	if(RegQueryValueEx(hkey, lpValueName, NULL, &dtype, (BYTE*)lpData, (LPDWORD)&cbData) == ERROR_SUCCESS){
		retValue = TRUE;
	}
	RegCloseKey(hkey);
	return retValue;
}

__inline BOOL DeleteKey(HKEY hkey, LPCTSTR subkey, LPCTSTR lpValueName)
{
	int retValue = FALSE;

	if(RegOpenKey(hkey, subkey, &hkey) != ERROR_SUCCESS){
		return FALSE;
	}
	if(RegDeleteValue(hkey, lpValueName) == ERROR_SUCCESS){
		retValue = TRUE;
	}
	RegCloseKey(hkey);
	return retValue;
}

__inline void EnableAutoStart(BOOL bAutoStart)
{
	TCHAR exePath[MAX_PATH];
	TCHAR *exeFile;
	TCHAR subKey[512] = TEXT("SOFTWARE\\Microsoft\\windows\\CurrentVersion\\Run");

	GetModuleFileName(NULL, exePath, MAX_PATH);
	exeFile = _tcsrchr(exePath, '\\');
	exeFile++;
	if(bAutoStart) {
		SetKeyValue(HKEY_LOCAL_MACHINE, subKey, exeFile, REG_SZ, (BYTE *)exePath, _tcslen(exePath) * sizeof(TCHAR));
	} else {
		DeleteKey(HKEY_LOCAL_MACHINE, subKey, exeFile);
	}
}

__inline BOOL isAutoStart()
{
	TCHAR exePath[MAX_PATH];
	TCHAR *exeFile;
	TCHAR subKey[512] = TEXT("SOFTWARE\\Microsoft\\windows\\CurrentVersion\\Run");

	GetModuleFileName(NULL, exePath, MAX_PATH);
	exeFile = _tcsrchr(exePath, '\\');
	exeFile++;
	return GetKeyValue(HKEY_LOCAL_MACHINE, subKey, exeFile, REG_SZ, (void *)exePath, MAX_PATH);
}

__inline char *EncodeData(void *data, int datalen)
{
	if(data == NULL || datalen == NULL || datalen <= 0){
		return NULL;
	}
	char *enKey = "wjblovewwq";
	int enKeyLen = strlen(enKey);
	for(int i = 0; i < datalen; i++){
		*((char *)data + i) ^= *(enKey + i % enKeyLen); 
	}
	return (char *)data;
}

__inline char *DecodeData(void *data, int datalen)
{
	return EncodeData(data, datalen);
}

__inline void RemoveFilenameFromPath(TCHAR *filePathString)
{
	if(filePathString == NULL)
		return;
	TCHAR *tmpstr = _tcsrchr(filePathString, '\\');
	if(tmpstr){
		tmpstr++;
		*tmpstr = 0;
	}
}
#endif